package org.example.service;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.common.ResultCode;
import org.example.common.config.JwtTokenUtils;
import org.example.dao.AdminDao;
import org.example.entity.Account;
import org.example.entity.Admin;
import org.example.exception.CustomException;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminService {

  @Resource
  private AdminDao adminDao;

  public List<Admin> GetAll(){
    return adminDao.getAll();
  }

  public Admin adminLogin(Admin admin) {
    //1.拿到用户传来的用户名，先到数据库查看这个用户名是否存在,如果不存在抛出异常：“用户不存在”
    Admin dbAdmin = adminDao.findByUsername(admin.getUserName());
    if (ObjectUtil.isEmpty(dbAdmin)){
      throw new CustomException(ResultCode.USER_ACCOUNT_ERROR);
    }
    //2.如果用户存在，则那用户输入的密码跟数据库找到的用户的密码比对，密码输入正确，允许登录
    String password = admin.getPassword(); //用户输入的密码
    String dbPassword = dbAdmin.getPassword(); // 数据库找到用户的密码
    if (!password.equalsIgnoreCase(dbPassword)){
      throw new CustomException(ResultCode.USER_ACCOUNT_ERROR);
    }
    return dbAdmin;
  }

  public Admin findById(Integer id) {
    return adminDao.findById(id);
  }

  public Admin adminRegister(Admin admin) {
    String userName = admin.getUserName();
    //1.先校验用户名是不是为空： 如果用户名，为空，抛出异常
    if (ObjectUtil.isEmpty(userName)){
      throw new CustomException(ResultCode.USERNAME_ISNULL);
    }
    if (ObjectUtil.isEmpty(admin.getAvatar())) {
      admin.setAvatar("http://localhost:9090/files/default.jpg");
    }
    if (ObjectUtil.isEmpty(admin.getPassword())){
      admin.setPassword("123456");
    }
    else {
      //没有重复值，就插入数据
      int affected = adminDao.insertSelective(admin);
      if (affected > 0) {
        //throw  new CustomException(ResultCode.SUCCESS);
        return admin;
      }
    }
    return null;
    //2.校验用户名的唯一性：
//    Admin dbAdmin = adminDao.findByName(userName);
//    if (ObjectUtil.isNotEmpty(dbAdmin)) {
//      throw new CustomException(ResultCode.USER_EXIST_ERROR);
//    }
//    adminDao.insertSelective(admin);
//    return admin;
  }

  public void delete(Integer id) {
    adminDao.deleteByPrimaryKey(id);
  }

  public Admin adminEdit(Admin admin) {
    String userName = admin.getUserName();
    //1.先校验用户名是不是为空： 如果用户名，为空，抛出异常
    if (ObjectUtil.isEmpty(userName)){
      throw new CustomException(ResultCode.USERNAME_ISNULL);
    }
    adminDao.updateByPrimaryKeySelective(admin);

    return admin;
  }

  public PageInfo<Admin> findPage(Admin search, Integer pageNum, Integer pageSize) {
    Account user = JwtTokenUtils.getCurrentUser();
    if (ObjectUtil.isEmpty(user)) {
      throw new CustomException(ResultCode.USER_NOT_LOGIN);
    }
    // 开启分页
    PageHelper.startPage(pageNum, pageSize);
    List<Admin> all = findByCondition(search);

    return PageInfo.of(all);
  }

  // 根据条件查询的方法
  public List<Admin> findByCondition(Admin search) {
    return adminDao.findBySearch(search);
  }

}